<template>
  <el-dialog v-model="dialogVisible" :title="Object.keys(editRecord).length ? '修改会话标题' : '设置会话标题'" width="500">
    <el-form :model="formData" label-width="100px">
      <el-form-item label="会话标题">
        <el-input v-model="formData.title" placeholder="请输入会话标题" />
      </el-form-item>
      <el-form-item label="AI模型" v-if="!Object.keys(editRecord).length">
        <el-select v-model="formData.aiModel" placeholder="Select">
          <el-option v-for="item in aiModels" :label="item.desc" :value="item.code" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleOk">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed, watch, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getAiModel } from '@/request/ai'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  editRecord: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['input', 'ok'])

const formData = ref({
  title: '',
  aiModel: ''
})

const dialogVisible = computed({
  get: () => props.modelValue,
  set: () => emit('update:modelValue', false)
})

const handleOk = () => {
  if (formData.value.title.trim() === '') {
    return ElMessage.error('请输入会话标题')
  }
  emit('ok', { ...props.editRecord, ...formData.value })
  dialogVisible.value = false
}

const aiModel = ref('')

const aiModels = ref([])
onMounted(async () => {
  const { data } =  await getAiModel()
  aiModels.value = data
  aiModel.value = data[0].code
  formData.value.aiModel = data[0].code
})
</script>

<style lang="less"></style>